package exam.CMB;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;


public class Main2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        student[] students = new student[n];
        String NAME_COMPARE = "zzzzzzzzzzz";
        for (int i = 0; i < n; i++) {
            String name = in.next();
            String chinese = in.next();
            String math = in.next();
            String english = in.next();
            String total = in.next();
            int total_compare;
            if (total.equals("?") && !chinese.equals("?") && !math.equals("?") && !english.equals("?")) {
                int totalScore = Integer.valueOf(chinese) + Integer.valueOf(math) + Integer.valueOf(english);
                total = String.valueOf(totalScore);
            }
            if (!total.equals("?") && chinese.equals("?") && !math.equals("?") && !english.equals("?")) {
                int chineseScore = Integer.valueOf(total) - Integer.valueOf(math) - Integer.valueOf(english);
                chinese = String.valueOf(chineseScore);
            } else if (!total.equals("?") && !chinese.equals("?") && math.equals("?") && !english.equals("?")) {
                int mathScore = Integer.valueOf(total) - Integer.valueOf(chinese) - Integer.valueOf(english);
                math = String.valueOf(mathScore);
            } else if (!total.equals("?") && !chinese.equals("?") && !math.equals("?") && english.equals("?")) {
                int englishScore = Integer.valueOf(total) - Integer.valueOf(chinese) - Integer.valueOf(math);
                english = String.valueOf(englishScore);
            }
            if (total.equals("?"))
                total_compare = -1;
            else
                total_compare = Integer.parseInt(total);
            if (name.equals("?"))
                students[i] = new student(name, chinese, math, english, total, total_compare, NAME_COMPARE);
            else
                students[i] = new student(name, chinese, math, english, total, total_compare, name);

        }
        List<student> collect = Arrays.stream(students).sorted(new Comparator<student>() {
            @Override
            public int compare(student o1, student o2) {
                if(o1.total_compare == o2.total_compare){
                    return o1.name_compare.compareTo(o2.name_compare);
                }else
                    return o2.total_compare - o1.total_compare;
            }
        }).collect(Collectors.toList());
        for (student student : collect) {
            System.out.print(student.name + " " + student.chinese + " " + student.math + " " + student.english + " " + student.total);
            System.out.println();
        }
    }
}

class student {
    String name;
    String chinese;
    String math;
    String english;
    String total;
    int total_compare;
    String name_compare;

    public student(String name, String chinese, String math, String english, String total, int total_compare, String name_compare) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
        this.total = total;
        this.total_compare = total_compare;
        this.name_compare = name_compare;
    }
}
